﻿namespace YidanSoft.Library.EditorUtility.WinControls
{
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.IO;
    using System.Reflection;
    using System.Runtime.InteropServices;
    using System.Threading;
    using System.Windows.Forms;
    using YidanSoft.Library.EditorUtility.Collections;
    using YidanSoft.Library.EditorUtility.General;
    using YidanSoft.Library.EditorUtility.Menus;
    using YidanSoft.Library.EditorUtility.Win32;

    [ToolboxBitmap(typeof(OutlookBar), "YidanSoft.Library.EditorUtility.WinControls.OutlookBar.bmp")]
    public class OutlookBar : Control
    {
        private int animationSpeed = 20;
        private const int ARROW_BUTTON_MARGIN = 5;
        private Bitmap backgroundBitmap = null;
        private const int BAND_HEADER_HEIGHT = 0x16;
        private OutlookBarBandCollection bands = null;
        private YidanSoft.Library.EditorUtility.WinControls.BorderType borderType = YidanSoft.Library.EditorUtility.WinControls.BorderType.None;
        private bool buttonPushed = false;
        private ContextMenu contextMenu = null;
        private int currentBandIndex = -1;
        private bool doScrollingLoop = false;
        private DrawState downArrowDrawState = DrawState.Normal;
        private bool downArrowPressed = false;
        private Rectangle downArrowRect;
        private bool downArrowVisible = false;
        private DrawState downFlatArrowState = DrawState.Normal;
        private bool downTimerTicking = false;
        private Cursor dragCursor = null;
        private int droppedPosition = -1;
        private int firstItem = 0;
        private bool flatArrowButtons = true;
        private bool forceHightlight = false;
        private int forceHightlightIndex = -1;
        private System.Windows.Forms.Timer highlightTimer = new System.Windows.Forms.Timer();
        private OutlookBarItemClickedHandler ItemClicked;
        private OutlookBarItemDroppedHandler ItemDropped;
        private const int LARGE_TOP_MARGIN = 10;
        private Point lastClickedPoint = Point.Empty;
        private int lastDrawnLineIndex = -1;
        private int lastHighlightedHeader = -1;
        private int lastHighlightedItem = -1;
        private const int LEFT_MARGIN = 5;
        private Color leftTopColor = Color.Empty;
        private bool needBackgroundBitmapResize = true;
        private bool paintedDropLineLastItem = false;
        private bool previousPressed = false;
        private OutlookBarPropertyChangedHandler PropertyChanged;
        private Color rightBottomColor = Color.Empty;
        private int selectedHeader = -1;
        private const int SMALL_TOP_MARGIN = 6;
        private DrawState upArrowDrawState = DrawState.Normal;
        private bool upArrowPressed = false;
        private Rectangle upArrowRect;
        private bool upArrowVisible = false;
        private DrawState upFlatArrowState = DrawState.Normal;
        private bool upTimerTicking = false;
        private const int X_SMALLICON_LABEL_OFFSET = 2;
        private const int Y_LARGEICON_LABEL_OFFSET = 3;
        private const int Y_LARGEICON_SPACING = 8;
        private const int Y_SMALLICON_SPACING = 10;

        public event OutlookBarItemClickedHandler ItemClicked
        {
            add
            {
                OutlookBarItemClickedHandler handler2;
                OutlookBarItemClickedHandler itemClicked = this.ItemClicked;
                do
                {
                    handler2 = itemClicked;
                    OutlookBarItemClickedHandler handler3 = (OutlookBarItemClickedHandler) Delegate.Combine(handler2, value);
                    itemClicked = Interlocked.CompareExchange<OutlookBarItemClickedHandler>(ref this.ItemClicked, handler3, handler2);
                }
                while (itemClicked != handler2);
            }
            remove
            {
                OutlookBarItemClickedHandler handler2;
                OutlookBarItemClickedHandler itemClicked = this.ItemClicked;
                do
                {
                    handler2 = itemClicked;
                    OutlookBarItemClickedHandler handler3 = (OutlookBarItemClickedHandler) Delegate.Remove(handler2, value);
                    itemClicked = Interlocked.CompareExchange<OutlookBarItemClickedHandler>(ref this.ItemClicked, handler3, handler2);
                }
                while (itemClicked != handler2);
            }
        }

        public event OutlookBarItemDroppedHandler ItemDropped
        {
            add
            {
                OutlookBarItemDroppedHandler handler2;
                OutlookBarItemDroppedHandler itemDropped = this.ItemDropped;
                do
                {
                    handler2 = itemDropped;
                    OutlookBarItemDroppedHandler handler3 = (OutlookBarItemDroppedHandler) Delegate.Combine(handler2, value);
                    itemDropped = Interlocked.CompareExchange<OutlookBarItemDroppedHandler>(ref this.ItemDropped, handler3, handler2);
                }
                while (itemDropped != handler2);
            }
            remove
            {
                OutlookBarItemDroppedHandler handler2;
                OutlookBarItemDroppedHandler itemDropped = this.ItemDropped;
                do
                {
                    handler2 = itemDropped;
                    OutlookBarItemDroppedHandler handler3 = (OutlookBarItemDroppedHandler) Delegate.Remove(handler2, value);
                    itemDropped = Interlocked.CompareExchange<OutlookBarItemDroppedHandler>(ref this.ItemDropped, handler3, handler2);
                }
                while (itemDropped != handler2);
            }
        }

        public event OutlookBarPropertyChangedHandler PropertyChanged
        {
            add
            {
                OutlookBarPropertyChangedHandler handler2;
                OutlookBarPropertyChangedHandler propertyChanged = this.PropertyChanged;
                do
                {
                    handler2 = propertyChanged;
                    OutlookBarPropertyChangedHandler handler3 = (OutlookBarPropertyChangedHandler) Delegate.Combine(handler2, value);
                    propertyChanged = Interlocked.CompareExchange<OutlookBarPropertyChangedHandler>(ref this.PropertyChanged, handler3, handler2);
                }
                while (propertyChanged != handler2);
            }
            remove
            {
                OutlookBarPropertyChangedHandler handler2;
                OutlookBarPropertyChangedHandler propertyChanged = this.PropertyChanged;
                do
                {
                    handler2 = propertyChanged;
                    OutlookBarPropertyChangedHandler handler3 = (OutlookBarPropertyChangedHandler) Delegate.Remove(handler2, value);
                    propertyChanged = Interlocked.CompareExchange<OutlookBarPropertyChangedHandler>(ref this.PropertyChanged, handler3, handler2);
                }
                while (propertyChanged != handler2);
            }
        }

        public OutlookBar()
        {
            this.bands = new OutlookBarBandCollection(this);
            this.Dock = DockStyle.Left;
            base.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw | ControlStyles.Opaque | ControlStyles.UserPaint, true);
            this.Font = SystemInformation.MenuFont;
            this.highlightTimer.Tick += new EventHandler(this.OnHighlightHeader);
            this.highlightTimer.Interval = 100;
            Stream manifestResourceStream = Assembly.GetAssembly(System.Type.GetType("YidanSoft.Library.EditorUtility.WinControls.OutlookBar")).GetManifestResourceStream("YidanSoft.Library.EditorUtility.Resources.DragCursor.cur");
            this.dragCursor = new Cursor(manifestResourceStream);
        }

        private void AnimateScroll(int From, int To)
        {
            if ((this.currentBandIndex != -1) && (this.bands.Count != 0))
            {
                Graphics graphics;
                int num2;
                OutlookBarBand band = this.bands[this.currentBandIndex];
                Debug.Assert((From >= 0) && (From < this.bands.Count));
                Debug.Assert((To >= 0) && (To < this.bands.Count));
                Rectangle viewPortRect = this.GetViewPortRect();
                Rectangle rectangle2 = new Rectangle(0, 0, viewPortRect.Width, 0x16);
                Rectangle drawingRect = new Rectangle(0, 0, viewPortRect.Width, viewPortRect.Height + (rectangle2.Height * 2));
                IntPtr dC = WindowsAPI.GetDC(base.Handle);
                IntPtr hDC = WindowsAPI.CreateCompatibleDC(dC);
                IntPtr ptr3 = WindowsAPI.CreateCompatibleDC(dC);
                IntPtr hObject = WindowsAPI.CreateCompatibleBitmap(dC, drawingRect.Width, drawingRect.Height);
                IntPtr ptr5 = WindowsAPI.CreateCompatibleBitmap(dC, drawingRect.Width, drawingRect.Height);
                IntPtr ptr6 = WindowsAPI.SelectObject(hDC, hObject);
                IntPtr ptr7 = WindowsAPI.SelectObject(ptr3, ptr5);
                if (To > From)
                {
                    graphics = Graphics.FromHdc(ptr3);
                }
                else
                {
                    graphics = Graphics.FromHdc(hDC);
                }
                this.DrawBandBitmap(hDC, this.bands[From], From, drawingRect);
                this.DrawBandBitmap(ptr3, this.bands[To], To, drawingRect);
                this.DrawHeader(graphics, To, new Rectangle(drawingRect.Left, drawingRect.Top, drawingRect.Width, drawingRect.Top + 0x16), Border3DStyle.RaisedInner);
                Rectangle headerRect = this.GetHeaderRect(From);
                Rectangle rectangle5 = this.GetHeaderRect(To);
                int height = headerRect.Height;
                if (To > From)
                {
                    for (num2 = rectangle5.Top - height; num2 > headerRect.Bottom; num2 -= height)
                    {
                        WindowsAPI.BitBlt(dC, viewPortRect.Left, headerRect.Bottom + 1, viewPortRect.Width, (num2 - headerRect.Bottom) - 1, hDC, 0, 0, 0xcc0020);
                        WindowsAPI.BitBlt(dC, viewPortRect.Left, num2, viewPortRect.Width, (viewPortRect.Bottom - num2) + height, ptr3, 0, 0, 0xcc0020);
                        Thread.Sleep(this.animationSpeed);
                    }
                }
                else
                {
                    Rectangle rectangle6 = new Rectangle(viewPortRect.Left, viewPortRect.Bottom, viewPortRect.Width, viewPortRect.Bottom - height);
                    for (num2 = headerRect.Top + 1; num2 < (rectangle6.Top - height); num2 += height)
                    {
                        WindowsAPI.BitBlt(dC, viewPortRect.Left, headerRect.Top, viewPortRect.Width, (num2 - headerRect.Top) - 1, ptr3, 0, 0, 0xcc0020);
                        WindowsAPI.BitBlt(dC, viewPortRect.Left, num2, viewPortRect.Width, viewPortRect.Bottom - num2, hDC, 0, 0, 0xcc0020);
                        Thread.Sleep(this.animationSpeed);
                    }
                }
                WindowsAPI.ReleaseDC(base.Handle, dC);
                WindowsAPI.DeleteDC(hDC);
                WindowsAPI.DeleteDC(ptr3);
                WindowsAPI.SelectObject(hDC, hObject);
                WindowsAPI.SelectObject(ptr3, ptr5);
                WindowsAPI.DeleteObject(hObject);
                WindowsAPI.DeleteObject(ptr5);
            }
        }

        private void DrawArrowButton(Rectangle buttonRect, ButtonState state)
        {
            if (!this.HasChild())
            {
                Graphics g = Graphics.FromHwnd(base.Handle);
                bool up = true;
                ScrollButton down = ScrollButton.Up;
                if (buttonRect.Equals(this.downArrowRect))
                {
                    down = ScrollButton.Down;
                    up = false;
                }
                if (this.flatArrowButtons && !this.HasChild())
                {
                    int num;
                    int num2;
                    this.GetVisibleRange(g, out num, out num2);
                    if ((up && (num == 0)) || !(up || (num2 != (this.bands[this.currentBandIndex].Items.Count - 1))))
                    {
                        return;
                    }
                    Point mousePosition = Control.MousePosition;
                    mousePosition = base.PointToClient(mousePosition);
                    if (buttonRect.Contains(mousePosition))
                    {
                        if (up)
                        {
                            this.UpFlatArrowState = DrawState.Hot;
                        }
                        else
                        {
                            this.DownFlatArrowState = DrawState.Hot;
                        }
                        this.DrawFlatArrowButton(g, buttonRect, up, DrawState.Hot);
                    }
                    else
                    {
                        if (up)
                        {
                            this.UpFlatArrowState = DrawState.Normal;
                        }
                        else
                        {
                            this.DownFlatArrowState = DrawState.Normal;
                        }
                        this.DrawFlatArrowButton(g, buttonRect, up, DrawState.Normal);
                    }
                }
                else
                {
                    ControlPaint.DrawScrollButton(g, buttonRect, down, state);
                }
                g.Dispose();
            }
        }

        private void DrawArrowButtons(Graphics g)
        {
            if (this.bands.Count != 0)
            {
                int num;
                int num2;
                this.GetVisibleRange(g, out num, out num2);
                Rectangle viewPortRect = this.GetViewPortRect();
                this.upArrowRect = new Rectangle(0, 0, SystemInformation.VerticalScrollBarWidth, SystemInformation.VerticalScrollBarWidth);
                this.downArrowRect = this.upArrowRect;
                this.upArrowRect.Offset((viewPortRect.Right - 5) - SystemInformation.VerticalScrollBarWidth, viewPortRect.Top + 5);
                this.downArrowRect.Offset((viewPortRect.Right - 5) - SystemInformation.VerticalScrollBarWidth, (viewPortRect.Bottom - 5) - SystemInformation.VerticalScrollBarWidth);
                if (num > 0)
                {
                    Debug.WriteLine("画向上的箭头");
                    if (this.flatArrowButtons)
                    {
                        this.DrawFlatArrowButton(g, this.upArrowRect, true, this.UpFlatArrowState);
                    }
                    else if (this.upArrowPressed)
                    {
                        ControlPaint.DrawScrollButton(g, this.upArrowRect, ScrollButton.Up, ButtonState.Pushed);
                    }
                    else
                    {
                        ControlPaint.DrawScrollButton(g, this.upArrowRect, ScrollButton.Up, ButtonState.Normal);
                    }
                    this.upArrowVisible = true;
                }
                else
                {
                    this.upArrowVisible = false;
                    this.UpFlatArrowState = DrawState.Normal;
                }
                if (num2 < (this.bands[this.currentBandIndex].Items.Count - 1))
                {
                    Debug.WriteLine("画向下的箭头");
                    if (this.flatArrowButtons)
                    {
                        this.DrawFlatArrowButton(g, this.downArrowRect, false, this.DownFlatArrowState);
                    }
                    else if (this.downArrowPressed)
                    {
                        ControlPaint.DrawScrollButton(g, this.downArrowRect, ScrollButton.Down, ButtonState.Pushed);
                    }
                    else
                    {
                        ControlPaint.DrawScrollButton(g, this.downArrowRect, ScrollButton.Down, ButtonState.Normal);
                    }
                    this.downArrowVisible = true;
                }
                else
                {
                    this.downArrowVisible = false;
                    this.DownFlatArrowState = DrawState.Normal;
                }
            }
        }

        private void DrawBackground(Graphics g)
        {
            Rectangle clientRectangle = base.ClientRectangle;
            if ((this.bands.Count == 0) || (this.currentBandIndex == -1))
            {
                g.FillRectangle(SystemBrushes.Control, clientRectangle);
            }
            else if (this.backgroundBitmap == null)
            {
                if ((this.currentBandIndex >= 0) && (this.bands[this.currentBandIndex] != null))
                {
                    using (SolidBrush brush = new SolidBrush(this.bands[this.currentBandIndex].Background))
                    {
                        if (this.HasChild())
                        {
                            Rectangle viewPortRect = this.GetViewPortRect();
                            g.ExcludeClip(viewPortRect);
                        }
                        g.FillRectangle(brush, clientRectangle);
                    }
                }
            }
            else
            {
                GDIUtil.StrechBitmap(g, clientRectangle, this.backgroundBitmap);
                if (this.needBackgroundBitmapResize)
                {
                    this.needBackgroundBitmapResize = false;
                    this.backgroundBitmap = GDIUtil.GetStrechedBitmap(g, clientRectangle, this.backgroundBitmap);
                }
            }
        }

        private void DrawBandBitmap(IntPtr hDC, OutlookBarBand band, int bandIndex, Rectangle drawingRect)
        {
            if (!this.HasChild(bandIndex))
            {
                YidanSoft.Library.EditorUtility.Win32.RECT rect;
                Color background = band.Background;
                IntPtr hBrush = WindowsAPI.CreateSolidBrush(ColorUtil.RGB(background.R, background.G, background.B));
                rect.left = drawingRect.Left;
                rect.top = drawingRect.Top;
                rect.right = drawingRect.Left + drawingRect.Width;
                rect.bottom = drawingRect.Top + drawingRect.Height;
                WindowsAPI.FillRect(hDC, ref rect, hBrush);
                WindowsAPI.DeleteObject(hBrush);
            }
            if (this.HasChild(bandIndex))
            {
                Control childControl = this.bands[bandIndex].ChildControl;
                bool visible = childControl.Visible;
                childControl.Visible = true;
                YidanSoft.Library.EditorUtility.Win32.POINT point = new YidanSoft.Library.EditorUtility.Win32.POINT();
                WindowsAPI.SendMessage(childControl.Handle, 20, (int) hDC, 0);
                WindowsAPI.SendMessage(childControl.Handle, 15, (int) hDC, 0);
                if (!visible)
                {
                    childControl.Visible = false;
                }
            }
            else
            {
                this.DrawItems(Graphics.FromHdc(hDC), drawingRect, this.bands[bandIndex]);
            }
        }

        private void DrawBorder(Graphics g)
        {
            Rectangle clientRectangle = base.ClientRectangle;
            if (this.borderType == YidanSoft.Library.EditorUtility.WinControls.BorderType.FixedSingle)
            {
                g.DrawRectangle(Pens.Black, clientRectangle.Left, clientRectangle.Top, clientRectangle.Width - 1, clientRectangle.Height - 1);
            }
            else if (this.borderType == YidanSoft.Library.EditorUtility.WinControls.BorderType.Fixed3D)
            {
                ControlPaint.DrawBorder3D(g, clientRectangle, Border3DStyle.Sunken);
            }
            else if (this.borderType == YidanSoft.Library.EditorUtility.WinControls.BorderType.Custom)
            {
                Pen pen = new Pen(this.leftTopColor);
                Pen pen2 = new Pen(this.RightBottomColor);
                g.DrawRectangle(pen, clientRectangle.Left, clientRectangle.Top, clientRectangle.Width - 1, clientRectangle.Height - 1);
                g.DrawRectangle(pen2, (int) (clientRectangle.Left + 1), (int) (clientRectangle.Top + 1), (int) (clientRectangle.Width - 3), (int) (clientRectangle.Height - 3));
                pen.Dispose();
                pen2.Dispose();
            }
        }

        private void DrawDropLine(Graphics g, int index, bool drawLine, YidanSoft.Library.EditorUtility.WinControls.HitTestType hit)
        {
            Brush controlText;
            Pen pen;
            int num2;
            if (drawLine)
            {
                this.droppedPosition = index;
                this.lastDrawnLineIndex = index;
                controlText = SystemBrushes.ControlText;
                pen = SystemPens.ControlText;
            }
            else
            {
                if (this.lastDrawnLineIndex == -1)
                {
                    return;
                }
                index = this.lastDrawnLineIndex;
                controlText = new SolidBrush(this.bands[this.currentBandIndex].Background);
                pen = new Pen(this.bands[this.currentBandIndex].Background);
                this.lastDrawnLineIndex = -1;
            }
            int count = this.bands[this.currentBandIndex].Items.Count;
            Rectangle rectangle = this.GetItemRect(g, this.bands[this.currentBandIndex], index, Rectangle.Empty);
            Rectangle viewPortRect = this.GetViewPortRect();
            if (this.bands[this.currentBandIndex].IconView == IconView.Small)
            {
                num2 = rectangle.Top - 5;
            }
            else
            {
                num2 = rectangle.Top - 8;
            }
            if (hit == YidanSoft.Library.EditorUtility.WinControls.HitTestType.DropLineLastItem)
            {
                num2 = this.GetLabelRect(count - 1).Bottom + 8;
                this.paintedDropLineLastItem = true;
                this.droppedPosition = count;
            }
            else if (this.paintedDropLineLastItem)
            {
                num2 = this.GetLabelRect(count - 1).Bottom + 8;
                this.paintedDropLineLastItem = false;
            }
            if ((this.backgroundBitmap != null) && (this.lastDrawnLineIndex == -1))
            {
                Rectangle destRect = new Rectangle(viewPortRect.Left, num2 - 6, viewPortRect.Width, 12);
                g.DrawImage(this.backgroundBitmap, destRect, destRect, GraphicsUnit.Pixel);
            }
            else
            {
                Point point3;
                Point point4;
                Point point = new Point(viewPortRect.Left + 11, num2);
                Point point2 = new Point(viewPortRect.Right - 11, num2);
                g.DrawLine(pen, point, point2);
                if (index == this.firstItem)
                {
                    point3 = new Point(viewPortRect.Left + 5, num2);
                }
                else
                {
                    point3 = new Point(viewPortRect.Left + 5, num2 - 6);
                }
                if (hit == YidanSoft.Library.EditorUtility.WinControls.HitTestType.DropLineLastItem)
                {
                    point4 = new Point(viewPortRect.Left + 5, num2 + 1);
                }
                else
                {
                    point4 = new Point(viewPortRect.Left + 5, num2 + 6);
                }
                Point point5 = new Point(viewPortRect.Left + 11, num2);
                Point[] points = new Point[3];
                points.SetValue(point3, 0);
                points.SetValue(point5, 1);
                points.SetValue(point4, 2);
                g.FillPolygon(controlText, points);
                if (index == this.firstItem)
                {
                    point3 = new Point(viewPortRect.Right - 5, num2);
                }
                else
                {
                    point3 = new Point(viewPortRect.Right - 5, num2 - 6);
                }
                if (hit == YidanSoft.Library.EditorUtility.WinControls.HitTestType.DropLineLastItem)
                {
                    point4 = new Point(viewPortRect.Right - 5, num2 + 1);
                }
                else
                {
                    point4 = new Point(viewPortRect.Right - 5, num2 + 6);
                }
                point5 = new Point(viewPortRect.Right - 11, num2);
                points.SetValue(point3, 0);
                points.SetValue(point5, 1);
                points.SetValue(point4, 2);
                g.FillPolygon(controlText, points);
                if (!drawLine)
                {
                    controlText.Dispose();
                    pen.Dispose();
                }
            }
        }

        private void DrawFlatArrowButton(Graphics g, Rectangle rc, bool up, DrawState state)
        {
            if (!this.HasChild())
            {
                Brush brush;
                if (state == DrawState.Hot)
                {
                    brush = new SolidBrush(ColorUtil.VSNetCheckedColor);
                }
                else
                {
                    brush = new SolidBrush(ColorUtil.VSNetControlColor);
                }
                g.FillRectangle(brush, rc.Left, rc.Top, rc.Width - 1, rc.Height - 1);
                brush.Dispose();
                using (Pen pen = new Pen(ColorUtil.VSNetBorderColor))
                {
                    if (state == DrawState.Hot)
                    {
                        g.DrawRectangle(pen, rc.Left, rc.Top, rc.Width - 2, rc.Height - 2);
                    }
                    GDIUtil.DrawArrowGlyph(g, rc, 7, 4, up, Brushes.Black);
                }
                if (up)
                {
                    this.UpFlatArrowState = state;
                }
                else
                {
                    this.DownFlatArrowState = state;
                }
            }
        }

        private void DrawHeader(Graphics g, int index, Rectangle rc, Border3DStyle style)
        {
            string text = this.bands[index].Text;
            using (Brush brush = new SolidBrush(ColorUtil.VSNetControlColor))
            {
                g.FillRectangle(brush, rc);
            }
            if (ColorUtil.UsingCustomColor)
            {
                Pen pen;
                if (style == Border3DStyle.RaisedInner)
                {
                    pen = new Pen(ColorUtil.VSNetBorderColor);
                    g.DrawRectangle(pen, rc.Left, rc.Top, rc.Width - 1, rc.Height - 1);
                }
                else
                {
                    using (Brush brush2 = new SolidBrush(ColorUtil.VSNetSelectionColor))
                    {
                        g.FillRectangle(brush2, rc);
                    }
                    pen = new Pen(ColorUtil.VSNetBorderColor);
                    g.DrawRectangle(pen, rc.Left, rc.Top, rc.Width - 1, rc.Height - 1);
                }
                pen.Dispose();
            }
            else
            {
                ControlPaint.DrawBorder3D(g, rc, style);
            }
            StringFormat format = new StringFormat();
            format.LineAlignment = StringAlignment.Center;
            format.Alignment = StringAlignment.Center;
            g.DrawString(text, this.Font, SystemBrushes.WindowText, rc, format);
        }

        private void DrawHeaders(Graphics g)
        {
            for (int i = 0; i < this.bands.Count; i++)
            {
                Rectangle headerRect = this.GetHeaderRect(i);
                this.DrawHeader(g, i, headerRect, Border3DStyle.RaisedInner);
            }
        }

        private void DrawItem(Graphics g, int index, Rectangle itemRect, bool hot, bool pressed, Rectangle targetRect, OutlookBarBand drawingBand)
        {
            Rectangle rectangle2;
            Brush brush;
            Pen pen;
            OutlookBarBand band = this.bands[this.currentBandIndex];
            if (drawingBand != null)
            {
                band = drawingBand;
            }
            Point location = new Point(0, 0);
            Rectangle viewPortRect = this.GetViewPortRect();
            if (targetRect != Rectangle.Empty)
            {
                viewPortRect = targetRect;
            }
            Region region = new Region(viewPortRect);
            g.Clip = region;
            g.ExcludeClip(this.downArrowRect);
            g.ExcludeClip(this.upArrowRect);
            Color textColor = band.TextColor;
            Color background = band.Background;
            Color color = band.Background;
            if (ColorUtil.UsingCustomColor)
            {
                background = ColorUtil.VSNetControlColor;
                color = ColorUtil.VSNetControlColor;
            }
            if (hot)
            {
                background = ColorUtil.VSNetSelectionColor;
                color = ColorUtil.VSNetBorderColor;
            }
            if (pressed)
            {
                background = ColorUtil.VSNetPressedColor;
            }
            if ((band.IconView == IconView.Large) && (band.LargeImageList != null))
            {
                Size imageSize = band.LargeImageList.ImageSize;
                location.X = itemRect.Left + ((viewPortRect.Width - imageSize.Width) / 2);
                location.Y = itemRect.Top;
                rectangle2 = new Rectangle(location, imageSize);
                using (brush = new SolidBrush(background))
                {
                    rectangle2.Inflate(2, 2);
                    if (this.backgroundBitmap != null)
                    {
                        g.DrawImage(this.backgroundBitmap, rectangle2, rectangle2, GraphicsUnit.Pixel);
                        if (hot)
                        {
                            g.FillRectangle(brush, rectangle2.Left, rectangle2.Top, rectangle2.Width, rectangle2.Height);
                        }
                    }
                    else
                    {
                        g.FillRectangle(brush, rectangle2.Left, rectangle2.Top, rectangle2.Width, rectangle2.Height);
                    }
                    using (pen = new Pen(color))
                    {
                        if ((this.backgroundBitmap == null) || hot)
                        {
                            g.DrawRectangle(pen, rectangle2.Left, rectangle2.Top, rectangle2.Width - 1, rectangle2.Height - 1);
                        }
                    }
                }
                if ((band.Items[index].ImageIndex != -1) && (band.LargeImageList != null))
                {
                    g.SmoothingMode = SmoothingMode.HighQuality;
                    g.DrawImage(band.LargeImageList.Images[band.Items[index].ImageIndex], location);
                    g.SmoothingMode = SmoothingMode.Default;
                }
                int y = (itemRect.Top + imageSize.Height) + 3;
                Size size2 = this.GetLabelSize(g, band, index);
                int x = itemRect.Left + ((viewPortRect.Width - size2.Width) / 2);
                using (brush = new SolidBrush(textColor))
                {
                    g.DrawString(band.Items[index].Text, this.Font, brush, (PointF) new Point(x, y));
                }
                region.Dispose();
                g.Clip = new Region(viewPortRect);
            }
            else if ((band.IconView == IconView.Small) && (band.SmallImageList != null))
            {
                Size size = band.SmallImageList.ImageSize;
                location.X = itemRect.Left;
                location.Y = itemRect.Top + ((itemRect.Height - size.Height) / 2);
                rectangle2 = new Rectangle(location, size);
                using (brush = new SolidBrush(background))
                {
                    rectangle2.Inflate(1, 1);
                    if (this.backgroundBitmap != null)
                    {
                        g.DrawImage(this.backgroundBitmap, rectangle2, rectangle2, GraphicsUnit.Pixel);
                        if (hot)
                        {
                            g.FillRectangle(brush, rectangle2.Left, rectangle2.Top, rectangle2.Width, rectangle2.Height);
                        }
                    }
                    else
                    {
                        g.FillRectangle(brush, rectangle2.Left, rectangle2.Top, rectangle2.Width, rectangle2.Height);
                    }
                    using (pen = new Pen(color))
                    {
                        if ((this.backgroundBitmap == null) || hot)
                        {
                            g.DrawRectangle(pen, rectangle2.Left, rectangle2.Top, rectangle2.Width - 1, rectangle2.Height - 1);
                        }
                    }
                }
                if ((band.Items[index].ImageIndex != -1) && (band.SmallImageList != null))
                {
                    g.DrawImage(band.SmallImageList.Images[band.Items[index].ImageIndex], location);
                }
                Size size4 = this.GetLabelSize(g, band, index);
                location.X = (location.X + size.Width) + 2;
                location.Y = itemRect.Top + ((itemRect.Height - size4.Height) / 2);
                using (brush = new SolidBrush(textColor))
                {
                    g.DrawString(band.Items[index].Text, this.Font, brush, (PointF) location);
                }
            }
        }

        private void DrawItems(Graphics g, Rectangle targetRect, OutlookBarBand drawBand)
        {
            if (this.bands.Count != 0)
            {
                Rectangle viewPortRect = this.GetViewPortRect();
                OutlookBarBand band = this.bands[this.currentBandIndex];
                if (drawBand != null)
                {
                    band = drawBand;
                }
                Debug.Assert(band != null);
                for (int i = this.firstItem; i < band.Items.Count; i++)
                {
                    Rectangle itemRect = this.GetItemRect(g, band, i, targetRect);
                    if (itemRect.Top > viewPortRect.Bottom)
                    {
                        break;
                    }
                    this.DrawItem(g, i, itemRect, false, false, targetRect, drawBand);
                }
            }
        }

        private void FireItemClicked(int index)
        {
            if (this.ItemClicked != null)
            {
                if (this.currentBandIndex == -1)
                {
                    this.SetCurrentBand(0);
                }
                if ((index >= 0) && (index < this.bands[this.currentBandIndex].Items.Count))
                {
                    this.ItemClicked(this.bands[this.currentBandIndex], this.bands[this.currentBandIndex].Items[index]);
                }
            }
        }

        private void FireItemDropped(int index)
        {
            if (this.ItemDropped != null)
            {
                if (this.currentBandIndex == -1)
                {
                    this.SetCurrentBand(0);
                }
                if ((index >= 0) && (index < this.bands[this.currentBandIndex].Items.Count))
                {
                    this.ItemDropped(this.bands[this.currentBandIndex], this.bands[this.currentBandIndex].Items[index]);
                }
            }
        }

        private void FirePropertyChanged(OutlookBarProperty property)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this.bands[this.currentBandIndex], property);
            }
        }

        private void ForceHighlightItem(Graphics g, int index)
        {
            Rectangle itemRect = this.GetItemRect(g, this.bands[this.currentBandIndex], index, Rectangle.Empty);
            this.DrawItem(g, index, itemRect, true, false, Rectangle.Empty, null);
        }

        public int GetCurrentBand()
        {
            return this.currentBandIndex;
        }

        private Rectangle GetHeaderRect(int index)
        {
            Debug.Assert((index >= 0) && (index <= (this.bands.Count - 1)), "Invalid Header Index");
            Rectangle workRect = this.GetWorkRect();
            int top = workRect.Top;
            int bottom = workRect.Bottom;
            int count = this.bands.Count;
            if (index > this.currentBandIndex)
            {
                top = (workRect.Bottom - 1) - ((count - index) * 0x16);
                bottom = (workRect.Bottom - 1) - (((count - index) - 1) * 0x16);
            }
            else
            {
                top = (workRect.Top + 1) + (index * 0x16);
                bottom = (workRect.Top + 1) + ((index + 1) * 0x16);
            }
            return new Rectangle(workRect.Left, top, workRect.Width, bottom - top);
        }

        private Rectangle GetIconRect(int index)
        {
            Rectangle viewPortRect = this.GetViewPortRect();
            OutlookBarBand band = this.bands[this.currentBandIndex];
            Debug.Assert(band != null);
            Size empty = Size.Empty;
            Rectangle rectangle2 = Rectangle.Empty;
            Point location = new Point(0, 0);
            using (Graphics graphics = Graphics.FromHwnd(base.Handle))
            {
                rectangle2 = this.GetItemRect(graphics, band, index, Rectangle.Empty);
            }
            if (band.IconView == IconView.Small)
            {
                if (band.SmallImageList != null)
                {
                    empty = band.SmallImageList.ImageSize;
                    location.X = rectangle2.Left;
                    location.Y = rectangle2.Top + ((rectangle2.Height - empty.Height) / 2);
                }
                return new Rectangle(location, empty);
            }
            if (band.LargeImageList != null)
            {
                empty = band.LargeImageList.ImageSize;
                location.X = rectangle2.Left + ((viewPortRect.Width - empty.Width) / 2);
                location.Y = rectangle2.Top;
            }
            return new Rectangle(location, empty);
        }

        private Size GetIconSize(OutlookBarBand band)
        {
            if ((band.IconView == IconView.Large) && (band.LargeImageList != null))
            {
                return band.LargeImageList.ImageSize;
            }
            if ((band.IconView == IconView.Small) && (band.SmallImageList != null))
            {
                return band.SmallImageList.ImageSize;
            }
            return new Size(0, 0);
        }

        public Rectangle GetItemRect(Graphics g, OutlookBarBand band, int index, Rectangle targetRect)
        {
            Rectangle viewPortRect = this.GetViewPortRect();
            if (targetRect != Rectangle.Empty)
            {
                viewPortRect = targetRect;
            }
            Size size = new Size(0, 0);
            int top = viewPortRect.Top;
            int num2 = 0;
            for (int i = 0; i < index; i++)
            {
                size = this.GetItemSize(g, band, i, ItemSizeType.All);
                top += size.Height;
                if (band.IconView == IconView.Small)
                {
                    top += 10;
                }
                else
                {
                    top += 8;
                }
                if (i == (this.firstItem - 1))
                {
                    num2 = top - viewPortRect.Top;
                }
            }
            size = this.GetItemSize(g, band, index, ItemSizeType.All);
            int num4 = 6;
            if (band.IconView == IconView.Large)
            {
                num4 = 10;
            }
            YidanSoft.Library.EditorUtility.Win32.RECT rect = new YidanSoft.Library.EditorUtility.Win32.RECT();
            rect.left = viewPortRect.Left;
            rect.top = top;
            rect.right = viewPortRect.Left + size.Width;
            rect.bottom = top + size.Height;
            rect.top -= num2;
            rect.bottom -= num2;
            rect.top += num4;
            rect.bottom += num4;
            if (band.IconView == IconView.Small)
            {
                rect.left = viewPortRect.Left + 5;
                rect.right = viewPortRect.Right;
            }
            return new Rectangle(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
        }

        private Size GetItemSize(Graphics g, OutlookBarBand band, int itemIndex, ItemSizeType itemSizeType)
        {
            Size iconSize = new Size(0, 0);
            Size size2 = new Size(0, 0);
            if ((itemSizeType == ItemSizeType.Icon) || (itemSizeType == ItemSizeType.All))
            {
                iconSize = this.GetIconSize(band);
                if (itemSizeType == ItemSizeType.Icon)
                {
                    return iconSize;
                }
            }
            if ((itemSizeType == ItemSizeType.Label) || (itemSizeType == ItemSizeType.All))
            {
                size2 = this.GetLabelSize(g, band, itemIndex);
                if (itemSizeType == ItemSizeType.Label)
                {
                    return size2;
                }
            }
            if (itemSizeType == ItemSizeType.All)
            {
                if (band.IconView == IconView.Small)
                {
                    return new Size((iconSize.Width + size2.Width) + 2, (iconSize.Height > size2.Height) ? iconSize.Height : size2.Height);
                }
                return new Size((iconSize.Width > size2.Width) ? iconSize.Width : size2.Width, ((iconSize.Height + size2.Height) + 3) + 8);
            }
            return new Size(0, 0);
        }

        private Rectangle GetLabelRect(int index)
        {
            Rectangle viewPortRect = this.GetViewPortRect();
            OutlookBarBand band = this.bands[this.currentBandIndex];
            Debug.Assert(band != null);
            Size empty = Size.Empty;
            Rectangle rectangle2 = Rectangle.Empty;
            Size size2 = Size.Empty;
            Point point = new Point(0, 0);
            using (Graphics graphics = Graphics.FromHwnd(base.Handle))
            {
                rectangle2 = this.GetItemRect(graphics, band, index, Rectangle.Empty);
                size2 = this.GetLabelSize(graphics, band, index);
            }
            if (band.IconView == IconView.Small)
            {
                if (band.SmallImageList != null)
                {
                    empty = band.SmallImageList.ImageSize;
                    point.X = rectangle2.Left + empty.Width;
                    point.Y = rectangle2.Top + ((rectangle2.Height - size2.Height) / 2);
                }
                return new Rectangle(point.X, point.Y, (size2.Width + 2) + 2, size2.Height);
            }
            if (band.LargeImageList != null)
            {
                empty = band.LargeImageList.ImageSize;
                point.X = rectangle2.Left + ((viewPortRect.Width - size2.Width) / 2);
                point.Y = rectangle2.Top + empty.Height;
            }
            return new Rectangle(point.X, point.Y, size2.Width, (size2.Height + 3) + 2);
        }

        private Size GetLabelSize(Graphics g, OutlookBarBand band, int itemIndex)
        {
            Size size = new Size(0, 0);
            if (band.IconView == IconView.Large)
            {
                Rectangle viewPortRect = this.GetViewPortRect();
                Rectangle rc = Rectangle.FromLTRB(viewPortRect.Left, viewPortRect.Top, viewPortRect.Width, viewPortRect.Top);
                if (band.Items[itemIndex].Text != null)
                {
                    size = TextUtil.GetTextSize(null, band.Items[itemIndex].Text, this.Font, ref rc, DrawTextFormatFlags.DT_CALCRECT | DrawTextFormatFlags.DT_WORDBREAK | DrawTextFormatFlags.DT_CENTER);
                }
                return size;
            }
            if (band.Items[itemIndex].Text != null)
            {
                size = TextUtil.GetTextSize(null, band.Items[itemIndex].Text, this.Font);
            }
            return size;
        }

        private Rectangle GetViewPortRect()
        {
            Rectangle workRect = this.GetWorkRect();
            if (this.bands.Count > 0)
            {
                int y = (workRect.Top + 1) + (0x16 * (this.currentBandIndex + 1));
                int num2 = (workRect.Bottom - 1) - (0x16 * ((this.bands.Count - this.currentBandIndex) - 1));
                return new Rectangle(workRect.Left, y, workRect.Width, num2 - y);
            }
            return workRect;
        }

        private void GetVisibleRange(Graphics g, out int first, out int last)
        {
            first = this.firstItem;
            last = 0;
            OutlookBarBand band = this.bands[this.currentBandIndex];
            Rectangle viewPortRect = this.GetViewPortRect();
            for (int i = this.firstItem; i < band.Items.Count; i++)
            {
                if (this.GetItemRect(g, band, i, Rectangle.Empty).Bottom > viewPortRect.Bottom)
                {
                    last = i - 1;
                    break;
                }
                last = i;
            }
        }

        private Rectangle GetWorkRect()
        {
            Rectangle clientRectangle = base.ClientRectangle;
            if (this.borderType != YidanSoft.Library.EditorUtility.WinControls.BorderType.None)
            {
                clientRectangle.Inflate(-2, -2);
            }
            return clientRectangle;
        }

        private void HandleRecreated(object sender, EventArgs e)
        {
            Control control = (Control) sender;
            if (WindowsAPI.GetParent(control.Handle) != base.Handle)
            {
                WindowsAPI.SetParent(control.Handle, base.Handle);
            }
        }

        private bool HasChild()
        {
            Control childControl = null;
            if ((this.currentBandIndex != -1) && (this.bands.Count > 0))
            {
                OutlookBarBand band = this.bands[this.currentBandIndex];
                childControl = band.ChildControl;
            }
            return (childControl != null);
        }

        private bool HasChild(int index)
        {
            Control childControl = null;
            if ((((index != -1) && (this.bands.Count > 0)) && (index >= 0)) && (index < this.bands.Count))
            {
                OutlookBarBand band = this.bands[index];
                childControl = band.ChildControl;
            }
            return (childControl != null);
        }

        private void HighlightHeader(int index)
        {
            if (this.lastHighlightedHeader != index)
            {
                Rectangle headerRect;
                Graphics g = Graphics.FromHwnd(base.Handle);
                if (this.lastHighlightedHeader >= 0)
                {
                    headerRect = this.GetHeaderRect(this.lastHighlightedHeader);
                    this.DrawHeader(g, this.lastHighlightedHeader, headerRect, Border3DStyle.RaisedInner);
                }
                this.lastHighlightedHeader = index;
                if (this.lastHighlightedHeader >= 0)
                {
                    headerRect = this.GetHeaderRect(this.lastHighlightedHeader);
                    this.DrawHeader(g, this.lastHighlightedHeader, headerRect, Border3DStyle.Raised);
                }
            }
        }

        private void HighlightItem(int index, bool pressed)
        {
            if ((this.lastHighlightedItem != index) || (this.previousPressed != pressed))
            {
                Graphics graphics;
                Rectangle rectangle;
                this.previousPressed = pressed;
                if (this.lastHighlightedItem >= 0)
                {
                    using (graphics = Graphics.FromHwnd(base.Handle))
                    {
                        rectangle = this.GetItemRect(graphics, this.bands[this.currentBandIndex], this.lastHighlightedItem, Rectangle.Empty);
                        this.DrawItem(graphics, this.lastHighlightedItem, rectangle, false, false, Rectangle.Empty, null);
                    }
                }
                this.lastHighlightedItem = index;
                if (this.lastHighlightedItem >= 0)
                {
                    using (graphics = Graphics.FromHwnd(base.Handle))
                    {
                        rectangle = this.GetItemRect(graphics, this.bands[this.currentBandIndex], this.lastHighlightedItem, Rectangle.Empty);
                        this.DrawItem(graphics, this.lastHighlightedItem, rectangle, true, pressed, Rectangle.Empty, null);
                    }
                }
            }
        }

        public YidanSoft.Library.EditorUtility.WinControls.HitTestType HitTest(Point point, out int index, bool doingDragging)
        {
            index = 0;
            if ((this.bands.Count != 0) && (this.currentBandIndex != -1))
            {
                int num;
                if (this.upArrowVisible && this.upArrowRect.Contains(point))
                {
                    return YidanSoft.Library.EditorUtility.WinControls.HitTestType.UpScroll;
                }
                if (this.downArrowVisible && this.downArrowRect.Contains(point))
                {
                    return YidanSoft.Library.EditorUtility.WinControls.HitTestType.DownScroll;
                }
                for (num = 0; num < this.bands.Count; num++)
                {
                    if (this.GetHeaderRect(num).Contains(point))
                    {
                        index = num;
                        return YidanSoft.Library.EditorUtility.WinControls.HitTestType.Header;
                    }
                }
                if (this.HasChild())
                {
                    return YidanSoft.Library.EditorUtility.WinControls.HitTestType.Nothing;
                }
                int count = this.bands[this.currentBandIndex].Items.Count;
                Rectangle viewPortRect = this.GetViewPortRect();
                Rectangle empty = Rectangle.Empty;
                Rectangle labelRect = Rectangle.Empty;
                for (num = this.firstItem; num < count; num++)
                {
                    empty = this.GetIconRect(num);
                    if (empty.Contains(point))
                    {
                        index = num;
                        return YidanSoft.Library.EditorUtility.WinControls.HitTestType.Item;
                    }
                    if (!((empty.Bottom <= viewPortRect.Bottom) || doingDragging))
                    {
                        break;
                    }
                    labelRect = this.GetLabelRect(num);
                    if (labelRect.Contains(point))
                    {
                        index = num;
                        return YidanSoft.Library.EditorUtility.WinControls.HitTestType.Item;
                    }
                    if (!((labelRect.Bottom <= viewPortRect.Bottom) || doingDragging))
                    {
                        break;
                    }
                    if (doingDragging)
                    {
                        Rectangle rectangle5 = new Rectangle(viewPortRect.Left, empty.Top - 8, viewPortRect.Width, 8);
                        if (rectangle5.Contains(point))
                        {
                            index = num;
                            return YidanSoft.Library.EditorUtility.WinControls.HitTestType.DropLine;
                        }
                        if ((num == (count - 1)) && (point.Y > labelRect.Bottom))
                        {
                            index = count - 1;
                            return YidanSoft.Library.EditorUtility.WinControls.HitTestType.DropLineLastItem;
                        }
                    }
                }
            }
            return YidanSoft.Library.EditorUtility.WinControls.HitTestType.Nothing;
        }

        public void OnContextMenu(object sender, EventArgs e)
        {
            OutlookBarBand band = this.bands[this.currentBandIndex];
            if (typeof(MenuItemEx).IsInstanceOfType(sender))
            {
                MenuItemEx ex = (MenuItemEx) sender;
                if (ex.Text == "Large Icons")
                {
                    band.IconView = IconView.Large;
                    ex.RadioCheck = true;
                    this.contextMenu.MenuItems[1].RadioCheck = false;
                }
                else if (ex.Text == "Small Icons")
                {
                    ex.RadioCheck = true;
                    band.IconView = IconView.Small;
                    this.contextMenu.MenuItems[0].RadioCheck = false;
                }
            }
            base.Invalidate();
        }

        protected override void OnDragEnter(DragEventArgs drgevent)
        {
            drgevent.Effect = DragDropEffects.Move | DragDropEffects.Copy | DragDropEffects.Scroll;
            base.OnDragEnter(drgevent);
        }

        protected override void OnDragOver(DragEventArgs drgevent)
        {
            drgevent.Effect = DragDropEffects.Move | DragDropEffects.Copy | DragDropEffects.Scroll;
            base.OnDragOver(drgevent);
        }

        private void OnHighlightHeader(object timeObject, EventArgs eventArgs)
        {
            Point mousePosition = Control.MousePosition;
            mousePosition = base.PointToClient(mousePosition);
            if (!base.ClientRectangle.Contains(mousePosition))
            {
                this.HighlightHeader(-1);
                this.HighlightItem(-1, false);
                this.highlightTimer.Stop();
            }
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            int num;
            base.OnMouseDown(e);
            YidanSoft.Library.EditorUtility.WinControls.HitTestType type = this.HitTest(new Point(e.X, e.Y), out num, false);
            if (e.Button == MouseButtons.Left)
            {
                switch (type)
                {
                    case YidanSoft.Library.EditorUtility.WinControls.HitTestType.UpScroll:
                    case YidanSoft.Library.EditorUtility.WinControls.HitTestType.DownScroll:
                        if (type == YidanSoft.Library.EditorUtility.WinControls.HitTestType.UpScroll)
                        {
                            this.ProcessArrowScrolling(this.upArrowRect, ref this.upArrowVisible, ref this.upArrowPressed, ref this.upTimerTicking);
                        }
                        else
                        {
                            this.ProcessArrowScrolling(this.downArrowRect, ref this.downArrowVisible, ref this.downArrowPressed, ref this.downTimerTicking);
                        }
                        return;

                    case YidanSoft.Library.EditorUtility.WinControls.HitTestType.Header:
                        this.ProcessHeaderHit(num);
                        break;

                    case YidanSoft.Library.EditorUtility.WinControls.HitTestType.Item:
                        this.FireItemClicked(num);
                        base.Invalidate();
                        break;
                }
            }
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            int num;
            base.OnMouseMove(e);
            if (e.Button == MouseButtons.Left)
            {
                base.DoDragDrop("xxx", DragDropEffects.Move | DragDropEffects.Copy | DragDropEffects.Scroll);
            }
            Point point = new Point(e.X, e.Y);
            YidanSoft.Library.EditorUtility.WinControls.HitTestType type = this.HitTest(point, out num, false);
            if ((type != YidanSoft.Library.EditorUtility.WinControls.HitTestType.Header) && (this.lastHighlightedHeader >= 0))
            {
                this.HighlightHeader(-1);
            }
            if ((type != YidanSoft.Library.EditorUtility.WinControls.HitTestType.Item) && (this.lastHighlightedItem >= 0))
            {
                this.HighlightItem(-1, false);
            }
            if ((((this.upArrowDrawState == DrawState.Hot) && (type != YidanSoft.Library.EditorUtility.WinControls.HitTestType.UpScroll)) || this.flatArrowButtons) && this.flatArrowButtons)
            {
                if (this.upArrowVisible && (this.UpFlatArrowState != DrawState.Normal))
                {
                    this.DrawArrowButton(this.upArrowRect, ButtonState.Normal);
                }
                else
                {
                    this.DrawArrowButton(this.upArrowRect, ButtonState.Normal);
                }
            }
            if ((((this.downArrowDrawState == DrawState.Hot) && (type == YidanSoft.Library.EditorUtility.WinControls.HitTestType.DownScroll)) || this.flatArrowButtons) && this.flatArrowButtons)
            {
                if (this.downArrowVisible && (this.DownFlatArrowState != DrawState.Normal))
                {
                    this.DrawArrowButton(this.downArrowRect, ButtonState.Normal);
                }
                else
                {
                    this.DrawArrowButton(this.downArrowRect, ButtonState.Normal);
                }
            }
            if (type == YidanSoft.Library.EditorUtility.WinControls.HitTestType.Header)
            {
                Cursor.Current = Cursors.Hand;
                this.HighlightHeader(num);
                this.highlightTimer.Start();
            }
            else if (type == YidanSoft.Library.EditorUtility.WinControls.HitTestType.Item)
            {
                this.HighlightItem(num, false);
                this.highlightTimer.Start();
            }
            else if (type == YidanSoft.Library.EditorUtility.WinControls.HitTestType.UpScroll)
            {
                if ((this.flatArrowButtons && this.upArrowVisible) && (this.UpFlatArrowState != DrawState.Hot))
                {
                    this.DrawArrowButton(this.upArrowRect, ButtonState.Pushed);
                }
            }
            else if (((type == YidanSoft.Library.EditorUtility.WinControls.HitTestType.DownScroll) && (this.flatArrowButtons && this.downArrowVisible)) && (this.DownFlatArrowState != DrawState.Hot))
            {
                this.DrawArrowButton(this.downArrowRect, ButtonState.Pushed);
            }
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            Graphics g = pe.Graphics;
            if ((this.currentBandIndex == -1) && (this.bands.Count > 0))
            {
                this.SetCurrentBand(0);
            }
            this.DrawBackground(g);
            this.DrawBorder(g);
            this.DrawHeaders(g);
            if (!this.HasChild())
            {
                this.DrawItems(g, Rectangle.Empty, null);
                this.DrawArrowButtons(g);
            }
            if (this.forceHightlight)
            {
                this.ForceHighlightItem(g, this.forceHightlightIndex);
                this.forceHightlight = false;
            }
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            if (this.HasChild())
            {
                Rectangle viewPortRect = this.GetViewPortRect();
                OutlookBarBand band = this.bands[this.currentBandIndex];
                band.ChildControl.Bounds = viewPortRect;
            }
            this.needBackgroundBitmapResize = true;
        }

        public void PerformClick(int index)
        {
            this.FireItemClicked(index);
        }

        private void ProcessArrowScrolling(Rectangle arrowButton, ref bool arrowVisible, ref bool arrowPressed, ref bool timerTicking)
        {
            base.Capture = true;
            timerTicking = true;
            this.buttonPushed = true;
            this.DrawArrowButton(arrowButton, ButtonState.Pushed);
            System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
            timer.Tick += new EventHandler(this.ScrollingTick);
            timer.Interval = 300;
            timer.Start();
            this.doScrollingLoop = true;
            while (this.doScrollingLoop)
            {
                YidanSoft.Library.EditorUtility.Win32.MSG msg = new YidanSoft.Library.EditorUtility.Win32.MSG();
                WindowsAPI.GetMessage(ref msg, 0, 0, 0);
                Point pt = new Point(0, 0);
                if ((msg.message == 0x200) || (msg.message == 0x202))
                {
                    pt = WindowsAPI.GetPointFromLPARAM((int) msg.lParam);
                }
                switch (msg.message)
                {
                    case 0x200:
                        if (!arrowButton.Contains(pt))
                        {
                            break;
                        }
                        if (!this.buttonPushed)
                        {
                            this.DrawArrowButton(arrowButton, ButtonState.Pushed);
                            arrowVisible = true;
                            arrowPressed = true;
                            this.buttonPushed = true;
                        }
                        goto Label_02ED;

                    case 0x202:
                        if (this.buttonPushed)
                        {
                            if (!this.flatArrowButtons)
                            {
                                this.DrawArrowButton(arrowButton, ButtonState.Normal);
                            }
                            this.buttonPushed = false;
                        }
                        arrowVisible = false;
                        if (arrowButton.Contains(pt))
                        {
                            Rectangle rectangle;
                            if (arrowButton.Equals(this.upArrowRect))
                            {
                                if (this.firstItem > 0)
                                {
                                    this.firstItem--;
                                    rectangle = this.GetViewPortRect();
                                    base.Invalidate(rectangle);
                                }
                            }
                            else if (arrowButton.Equals(this.downArrowRect))
                            {
                                using (Graphics graphics = Graphics.FromHwnd(base.Handle))
                                {
                                    OutlookBarBand band = this.bands[this.currentBandIndex];
                                    if (band != null)
                                    {
                                        Rectangle rectangle2 = this.GetItemRect(graphics, band, band.Items.Count - 1, Rectangle.Empty);
                                        rectangle = this.GetViewPortRect();
                                        if (rectangle2.Bottom > rectangle.Bottom)
                                        {
                                            this.firstItem++;
                                            base.Invalidate(rectangle);
                                        }
                                    }
                                }
                            }
                        }
                        this.doScrollingLoop = false;
                        goto Label_02ED;

                    case 0x100:
                        if (((int) msg.wParam) == 0x1b)
                        {
                            this.doScrollingLoop = false;
                        }
                        goto Label_02ED;

                    default:
                        WindowsAPI.DispatchMessage(ref msg);
                        goto Label_02ED;
                }
                if (this.buttonPushed)
                {
                    this.DrawArrowButton(arrowButton, ButtonState.Normal);
                    arrowVisible = false;
                    arrowPressed = false;
                    this.buttonPushed = false;
                }
            Label_02ED:;
            }
            base.Capture = false;
            timer.Stop();
            timer.Dispose();
            arrowVisible = false;
            arrowPressed = false;
            timerTicking = false;
            Rectangle viewPortRect = this.GetViewPortRect();
            base.Invalidate(viewPortRect);
        }

        private void ProcessHeaderHit(int index)
        {
            base.Capture = true;
            Rectangle headerRect = this.GetHeaderRect(index);
            Graphics g = Graphics.FromHwnd(base.Handle);
            bool flag = true;
            this.DrawHeader(g, index, headerRect, Border3DStyle.Sunken);
            bool flag2 = true;
            while (flag2)
            {
                YidanSoft.Library.EditorUtility.Win32.MSG msg = new YidanSoft.Library.EditorUtility.Win32.MSG();
                WindowsAPI.GetMessage(ref msg, 0, 0, 0);
                Point pointFromLPARAM = new Point(0, 0);
                if ((msg.message == 0x200) || (msg.message == 0x202))
                {
                    pointFromLPARAM = WindowsAPI.GetPointFromLPARAM((int) msg.lParam);
                }
                switch (msg.message)
                {
                    case 0x200:
                        int num;
                        if ((this.HitTest(pointFromLPARAM, out num, false) != YidanSoft.Library.EditorUtility.WinControls.HitTestType.Header) || (num != index))
                        {
                            break;
                        }
                        if (!flag)
                        {
                            this.DrawHeader(g, index, headerRect, Border3DStyle.Sunken);
                            flag = true;
                        }
                        goto Label_0190;

                    case 0x202:
                        int num2;
                        if (flag)
                        {
                            this.DrawHeader(g, index, headerRect, Border3DStyle.RaisedInner);
                            flag = false;
                        }
                        if ((this.HitTest(pointFromLPARAM, out num2, false) == YidanSoft.Library.EditorUtility.WinControls.HitTestType.Header) && (num2 != this.selectedHeader))
                        {
                            this.SetCurrentBand(num2);
                        }
                        flag2 = false;
                        goto Label_0190;

                    case 0x100:
                        if (((int) msg.wParam) == 0x1b)
                        {
                            flag2 = false;
                        }
                        goto Label_0190;

                    default:
                        WindowsAPI.DispatchMessage(ref msg);
                        goto Label_0190;
                }
                if (flag)
                {
                    this.DrawHeader(g, index, headerRect, Border3DStyle.RaisedInner);
                    flag = false;
                }
            Label_0190:;
            }
            base.Capture = false;
            g.Dispose();
        }

        public void ProcessOnMouseDown(MouseEventArgs e)
        {
            this.OnMouseDown(e);
        }

        private void ScrollingTick(object timeObject, EventArgs eventArgs)
        {
            Rectangle viewPortRect;
            Point mousePosition = Control.MousePosition;
            mousePosition = base.PointToClient(mousePosition);
            if (this.upTimerTicking)
            {
                if (this.buttonPushed)
                {
                    if (this.upArrowRect.Contains(mousePosition))
                    {
                        this.upArrowPressed = true;
                        if (this.firstItem > 0)
                        {
                            this.firstItem--;
                            viewPortRect = this.GetViewPortRect();
                            base.Invalidate(viewPortRect);
                        }
                        else
                        {
                            this.doScrollingLoop = false;
                        }
                    }
                    else
                    {
                        this.upArrowPressed = false;
                    }
                }
            }
            else if (this.downTimerTicking)
            {
                if (this.buttonPushed)
                {
                    if (this.downArrowRect.Contains(mousePosition))
                    {
                        this.downArrowPressed = true;
                        using (Graphics graphics = Graphics.FromHwnd(base.Handle))
                        {
                            OutlookBarBand band = this.bands[this.currentBandIndex];
                            Rectangle rectangle2 = this.GetItemRect(graphics, band, band.Items.Count - 1, Rectangle.Empty);
                            viewPortRect = this.GetViewPortRect();
                            if (rectangle2.Bottom > viewPortRect.Bottom)
                            {
                                this.firstItem++;
                                base.Invalidate(viewPortRect);
                            }
                        }
                    }
                    else
                    {
                        this.doScrollingLoop = false;
                    }
                }
                else
                {
                    this.downArrowPressed = false;
                }
            }
        }

        public void SetCurrentBand(int index)
        {
            if (!base.DesignMode || (index != -1))
            {
                Debug.Assert((index >= 0) && (index < this.bands.Count), "Invalid Band Index");
                if (this.currentBandIndex != index)
                {
                    Control childControl;
                    if ((this.currentBandIndex != -1) && (this.bands.Count > 0))
                    {
                        OutlookBarBand band = this.bands[this.currentBandIndex];
                        childControl = band.ChildControl;
                        if (childControl != null)
                        {
                            childControl.Visible = false;
                        }
                    }
                    if (index != this.currentBandIndex)
                    {
                        this.AnimateScroll(this.currentBandIndex, index);
                    }
                    this.currentBandIndex = index;
                    this.firstItem = 0;
                    this.lastHighlightedItem = -1;
                    OutlookBarBand band2 = this.bands[this.currentBandIndex];
                    childControl = band2.ChildControl;
                    if (childControl != null)
                    {
                        if (WindowsAPI.GetParent(childControl.Handle) != base.Handle)
                        {
                            WindowsAPI.SetParent(childControl.Handle, base.Handle);
                            childControl.HandleCreated += new EventHandler(this.HandleRecreated);
                        }
                        Rectangle viewPortRect = this.GetViewPortRect();
                        childControl.Bounds = viewPortRect;
                        childControl.Visible = true;
                        childControl.Focus();
                    }
                    base.Invalidate();
                    this.FirePropertyChanged(OutlookBarProperty.CurrentBandChanged);
                }
            }
        }

        protected override void WndProc(ref Message m)
        {
            bool flag = true;
            if (m.Msg == 0x7b)
            {
                Point pointFromLPARAM = WindowsAPI.GetPointFromLPARAM((int) m.LParam);
                pointFromLPARAM = base.PointToClient(pointFromLPARAM);
                Rectangle viewPortRect = this.GetViewPortRect();
                this.lastClickedPoint = pointFromLPARAM;
                if (this.HasChild() && viewPortRect.Contains(pointFromLPARAM))
                {
                    flag = false;
                }
            }
            if (flag)
            {
                base.WndProc(ref m);
            }
        }

        public int AnimationSpeed
        {
            get
            {
                return this.animationSpeed;
            }
            set
            {
                this.animationSpeed = value;
            }
        }

        public Bitmap BackgroundBitmap
        {
            get
            {
                return this.backgroundBitmap;
            }
            set
            {
                this.backgroundBitmap = value;
                this.needBackgroundBitmapResize = true;
            }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public OutlookBarBandCollection Bands
        {
            get
            {
                return this.bands;
            }
        }

        public YidanSoft.Library.EditorUtility.WinControls.BorderType BorderType
        {
            get
            {
                return this.borderType;
            }
            set
            {
                this.borderType = value;
            }
        }

        protected override Size DefaultSize
        {
            get
            {
                return new Size(100, 10);
            }
        }

        internal DrawState DownFlatArrowState
        {
            get
            {
                return this.downFlatArrowState;
            }
            set
            {
                this.downFlatArrowState = value;
            }
        }

        public bool FlatArrowButtons
        {
            get
            {
                return this.flatArrowButtons;
            }
            set
            {
                this.flatArrowButtons = value;
            }
        }

        public Color LeftTopColor
        {
            get
            {
                return this.leftTopColor;
            }
            set
            {
                this.leftTopColor = value;
            }
        }

        public Color RightBottomColor
        {
            get
            {
                return this.rightBottomColor;
            }
            set
            {
                this.rightBottomColor = value;
            }
        }

        internal DrawState UpFlatArrowState
        {
            get
            {
                return this.upFlatArrowState;
            }
            set
            {
                this.upFlatArrowState = value;
            }
        }
    }
}

